package org.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class test extends ListActivity {

	private String CourseNumber = "";
	ArrayList<String> stringList = new ArrayList<String>();

	private String Book[] = new String[10];
	private String ISBN[] = new String[10];
	int counter = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		String result = "";

		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}
		CourseNumber = (String) extras.getSerializable("CourseNumber");
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters
				.add(new BasicNameValuePair("CourseNumber", CourseNumber));

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/GetBooksDetailsGivingCourseNumber.php");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(postParameters));

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		try {
			JSONArray jArray = new JSONArray(result);

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				Log.i("log_tag", " ISBN: " + json_data.getString("ISBN")
						+ ", Author: " + json_data.getString("Author")
						+ ", Title: " + json_data.getString("Title")
						+ ", Publisher: " + json_data.getString("Publisher")

				);
				ISBN[counter] = json_data.getString("ISBN");
				Book[counter] = " ISBN: " + json_data.getString("ISBN")
						+ " \n Author: " + json_data.getString("Author")
						+ "\n Title: " + json_data.getString("Title")
						+ "\n Publisher: " + json_data.getString("Publisher");
				stringList.add(Book[counter]);
				counter++;
			}

		} catch (JSONException e) {
			System.out.println("NO RECORDS FOUND");
		}

		setListAdapter(new ArrayAdapter<String>(this, R.layout.test, stringList));
		ListView listView = getListView();

		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String str = ((TextView) view).getText().toString();
				String[] split = str.split("\\n");
				String isbn = split[0].substring(6, split[0].length());

				Intent i = new Intent(test.this,
						GetSingleBookAndStudentDetailsActivity.class);
				i.putExtra("CourseNumber1", CourseNumber);
				i.putExtra("CourseNumber", ((TextView) view).getText());
				i.putExtra("flag", "0");
				i.putExtra("ISBN", isbn);
				startActivity(i);
			}

		});

	}

	public void Back(View view) throws Exception {
		Intent i = new Intent(this, BuySell8Activity.class);

		startActivity(i);
	}

	public void onClick1(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[0]);
		startActivity(i);
	}

	public void onClick2(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[1]);
		startActivity(i);
	}

	public void onClick3(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[2]);
		startActivity(i);
	}

	public void onClick4(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[3]);
		startActivity(i);
	}

}